<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'id',
        'xcx_openid',
        'mobile'
    ];

    //******
    public function getAvatarUrlAttribute() {

        $avatar = Avatar::where('user_id', $this->attributes['id'])->orderBy('id', 'desc')->first();

        $qiniu_bucket_url = "http://7xohc2.com1.z0.glb.clouddn.com/";

        if (is_null($avatar)) {
            $avatar_url = $qiniu_bucket_url."defaultUserAvatarImage";
        } else {
            $avatar_url = $qiniu_bucket_url.$avatar->uuid;
        }

        return $avatar_url;

    }

    public function getPlacesAttribute() {

        $places = Place::where('user_id', $this->attributes['id'])
            ->get();

        return $places;

    }

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['avatar_url', 'places'];


}
